import configApi, { ConfigType } from '@/api/config'

const unitOptions = ref<Array<{ label: string; value: string }>>([])

// 导出初始化方法
export const initUnitConfig = async () => {
  try {
    const res = await configApi.getConfigInfo(ConfigType.UNIT)
    unitOptions.value = res.data.map((item) => ({
      label: item.codeName,
      value: item.codeValue,
    }))
  } catch (err) {
    console.error('单位配置加载失败', err)
  }
}

const formatUnit = (unit: string) => {
  return unitOptions.value.find((item) => item.value === unit)?.label || ''
}

export { formatUnit }
